<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Chat as chatModel;
use think\facade\Db;
use think\facade\View;

class Chat extends Base{
    // 列表
    public function index(){
        $keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];
        $rs = chatModel::index($this->lang,$keyword,$order);
        
        View::assign('list',$rs);
        View::assign('total',$rs->total());
        View::assign('lastPage',$rs->lastPage());
        
        return view();
    }

    // 添加编辑
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = chatModel::find($id);
            View::assign('rs',$rs);

            // 客服属性
            $attributeWhere[] = ['tabledir','=','Chat']; $attributeWhere[] = ['lang','=',$this->lang];
            View::assign('attribute',Db::name('attribute')->where($attributeWhere)->order('sequence desc')->select());

			return view();
        }else{
            try {
                $rs = chatModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => $rs]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}
	
	// 单条删除
    public function delete_one(){
        $id = input('id');
        $chat = chatModel::find($id);
        
        try {
            $chat->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	/* 批量删除，返回值：
    @ error:no_check 未勾选数据
    @ success 成功
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)) return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
		
	    try {
            chatModel::destroy($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	

	// 批量更新
    public function update_all(){
		$id = input('id/a'); $sequence = input('sequence/a'); $title = input('title/a'); $account = input('account/a');
        
		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'sequence'=>$sequence[$i],'title'=>$title[$i],'account'=>$account[$i]];
        }
        
        try {
            $chat = new chatModel;
            $chat->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => '']);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}